﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VISS.API.Users;
using NCT.Framework;
using VISS.Web.BackEnd;


namespace VISS.Web.BackEnd.Modules.User {

    public partial class UserListControl : System.Web.UI.UserControl {

        public List<UserAdminEnt> UserList { get; set; }

        protected void Page_Load(object sender, EventArgs e) {
            if (UserList != null && UserList.Count > 0) {
                dtg_User.DataSource = UserList;
                dtg_User.DataBind();
            }
        }

        protected void dtg_User_RowDataBound(object sender, GridViewRowEventArgs e) {
            if (e.Row.RowIndex == -1)
                return;
            UserAdminEnt item = (UserAdminEnt)e.Row.DataItem;

            // Set Avatar
            if (item.AvatarFileName != string.Empty && item.AvatarStatus == EUserAvatarStatus.Available) {
                e.Row.Cells[0].Text = "<a href=\"http://viss.vn/profile?u=" + item.UserKey + "\" target=\"blank\" ><img src=\"" + ImageUtils.GetAvatarThumbPath(item.AvatarFileName) + "\" width=\"120\" height=\"120\" /></a>";
            }
            else {
                e.Row.Cells[0].Text = "<a href=\"http://viss.vn/profile?u=" + item.UserKey + "\" target=\"blank\" ><img src=\"" + ImageUtils.GetDefaultAvatarThumbPath(item.Gender) + "\" width=\"120\" height=\"120\" /></a>";
            }

            // Set Account Info
            string info = string.Empty;
            info += "Tên: " + item.Name + "<br />";
            info += "Giới tính: ";
            if (item.Gender == EUserGender.Male) {
                info += "Nam";
            }
            else if (item.Gender == EUserGender.Female) {
                info += "Nữ";
            }
            info += "<br />";
            info += "Email: " + item.Email + "<br />";
            info += "SDT: " + item.MobiPhone + "<br />";
            info += "Đăng nhập cuối: ";
            if (item.LastLogin > DateTimeUtils.MinValue) {
                info += item.LastLogin.ToString("dd/MM/yyyy hh:mm");
            }
            else {
                info += "Chưa";
            }
            info += "<br />";
            info += "Bị báo vi phạm: <a href=\"vi_pham?key=" + item.UserKey + "\" target=\"_blank\">" + item.ReportedTime + " lần </a>";
            info += "<br />";

            //Set link
            info += "<a href=\"xem_chi_tiet?key=" + item.UserKey + "\" target=\"_blank\">Xem chi tiết</a><br />";
            info += "<a href=\"\">Xem hình</a><br />";
            info += "<a href=\"tin_nhan?key=" + item.UserKey + "\" target=\"_blank\">Xem tin nhắn đã gửi</a>";

            e.Row.Cells[1].Text = info;

            //Set Action
            string action = string.Empty;
            bool canDoOtherAction = true;
            string activeAction = "<input type=\"checkbox\" id=\"chkActive-" + item.UserKey + "\" onclick=\"UserActive('" + item.UserKey + "');\" {0} /><label for=\"chkActive-" + item.UserKey + "\">Kích hoạt</label>";
            if ((item.Status & EUserStatus.Actived) == EUserStatus.Actived) {
                action += string.Format(activeAction, "checked=\"checked\" disabled");
            }
            else {
                action += string.Format(activeAction, "");
                canDoOtherAction = false;
            }
            action += "<br />";

            string verifyAction = "<input type=\"checkbox\" id=\"chkVerify-" + item.UserKey + "\" onclick=\"UserVerify('" + item.UserKey + "');\" {0} /><label for=\"chkVerify-" + item.UserKey + "\">Duyệt</label>"; ;
            if ((item.Status & EUserStatus.Verified) == EUserStatus.Verified) {
                action += string.Format(verifyAction, "checked=\"checked\"");
            }
            else {
                if (!canDoOtherAction) {
                    action += string.Format(verifyAction, "disabled");
                }
                else {
                    action += string.Format(verifyAction, "");
                }
            }
            action += "<br />";

            string disableAction = "<input type=\"checkbox\" id=\"chkDisable-" + item.UserKey + "\" onclick=\"UserDisable('" + item.UserKey + "');\" {0} /><label for=\"chkDisable-" + item.UserKey + "\">Khóa</label>"; ;
            if ((item.Status & EUserStatus.Disabled) == EUserStatus.Disabled) {
                action += string.Format(disableAction, "checked=\"checked\"");
            }
            else {
                if (!canDoOtherAction) {
                    action += string.Format(disableAction, "disabled");
                }
                else {
                    action += string.Format(disableAction, "");
                }
            }
            action += "<br />";

            string resetPasswordAction = "<input type=\"button\" onclick=\"ResetPassword('" + item.UserKey + "');\" value=\"Reset mật khẩu\" />";
            action += resetPasswordAction;

            e.Row.Cells[3].Text = action;
        }
    }
}
